package com.ruoyi.experiment.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.experiment.domain.Experiment;
import com.ruoyi.system.api.RemoteProjectResearchService;
import com.ruoyi.system.api.domain.ProjectResearch;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.experiment.service.IExperimentService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 实验管理Controller
 * 
 * @author gw
 * @date 2025-01-08
 */
@RestController
@RequestMapping("/experiment")
public class ExperimentController extends BaseController
{
    @Autowired
    private IExperimentService experimentService;

    @Autowired
    private RemoteProjectResearchService remoteProjectResearchService;

    /**
     * 查询实验管理列表
     */
    @RequiresPermissions("Experiment:Experiment:list")
    @GetMapping("/list")
    public TableDataInfo list(Experiment experiment)
    {
        startPage();
        List<Experiment> list = experimentService.selectExperimentList(experiment);
        for (Experiment experiment1 :list){
            R<ProjectResearch> researchR = remoteProjectResearchService.getProjectInfo(experiment1.getProjectId());
            ProjectResearch research=researchR.getData();
            experiment1.setProjectName(research.getProjectName());
        }
        return getDataTable(list);
    }

    /**
     * 查询实验管理列表
     */
    @PostMapping("/acaExperimentList")
    public List<Experiment> list1(Experiment experiment)
    {
        List<Experiment> list = experimentService.selectExperimentList(experiment);
        for (Experiment experiment1 :list){
            R<ProjectResearch> researchR = remoteProjectResearchService.getProjectInfo(experiment1.getProjectId());
            ProjectResearch research=researchR.getData();
            experiment1.setProjectName(research.getProjectName());
        }
        return list;
    }

    /**
     * 导出实验管理列表
     */
    @RequiresPermissions("Experiment:Experiment:export")
    @Log(title = "实验管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Experiment experiment)
    {
        List<Experiment> list = experimentService.selectExperimentList(experiment);
        ExcelUtil<Experiment> util = new ExcelUtil<Experiment>(Experiment.class);
        util.exportExcel(response, list, "实验管理数据");
    }

    /**
     * 获取实验管理详细信息
     */
    @RequiresPermissions("Experiment:Experiment:query")
    @GetMapping(value = "/{experimentId}")
    public AjaxResult getInfo(@PathVariable("experimentId") Long experimentId)
    {
        return success(experimentService.selectExperimentByExperimentId(experimentId));
    }

    /**
     * 新增实验管理
     */
    @RequiresPermissions("Experiment:Experiment:add")
    @Log(title = "实验管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Experiment experiment)
    {
        return toAjax(experimentService.insertExperiment(experiment));
    }

    /**
     * 修改实验管理
     */
    @RequiresPermissions("Experiment:Experiment:edit")
    @Log(title = "实验管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Experiment experiment)
    {
        return toAjax(experimentService.updateExperiment(experiment));
    }

    /**
     * 删除实验管理
     */
    @RequiresPermissions("Experiment:Experiment:remove")
    @Log(title = "实验管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{experimentIds}")
    public AjaxResult remove(@PathVariable Long[] experimentIds)
    {
        return toAjax(experimentService.deleteExperimentByExperimentIds(experimentIds));
    }
}
